﻿<?php
require_once("../include/db_info.inc.php");
if (!isset($_SESSION['administrator']) && !isset($_SESSION['balloon']))
{
	echo "<a href='../loginpage.php' target='main'>Didn't login or Have no permission!<br/>Login Admin And Then Reload This Page.</a>";
	exit(1);
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<html>
	<head>
		<meta http-equiv="refresh" content="20" url=<?php $cccid=intval($_GET['cid']);echo "balloon.php?cid=".strval($cccid);?>/>
<title>气球管理</title>
<script type="text/javascript">
	function change(obj){
		var oid=obj.id;
		if (obj.bgColor != "#aaffaa" && obj.bgColor != "aaffaa") return;
		window.location.href="balloon.php?bj="+oid+"&qxbj=&cid="+document.getElementById("ccid").value; 
	}
</script>
</head>
<font size="+2" face="微软雅黑">气球管理</font>
Designer : <a href='http://hi.baidu.com/%E9%83%AD%E5%A4%A7%E4%BE%A0%E7%88%B1%E5%86%99%E4%BB%A3%E7%A0%81/home'>CSGrandeur</a>&swimming</br>
<li><a href="index.php">返回管理页</a></li>
<li>如果未正常显示，请检查网址结尾"balloon.php"后是否加上了比赛id："?cid=xxxx"</li>

<li><font style="background:#eeeeee">灰色</font>是未交题，<font style="background:#ffaaaa">红色</font>为已提交但不正确；<b><font style="background:#aaffaa">绿色</font>为提交正确题目，需要发气球</b>；标记后为<font style="background:#1874cd">蓝色</font>，表示已发气球。</li>

</html>
<body onload="document.all.bj.focus()" >
<input type="hidden" id="hidd" value="0" />	
</body>


<form method="get"action="balloon.php">
已发气球标记：<input type="text" name="bj" />
<br />
取消标记：<input type="text" name="qxbj" />
<br />
<input type="submit" value="提交" />

<?php
	$now = time ();
	$cid=intval($_GET['cid']);
	echo "<input type='hidden' id='ccid' name='cid' value='".$cid."'/></form>";
	$file = "cache/contestrank$cid.html";
	if (file_exists ( $file ))
		$last = filemtime ( $file );
	else
		$last =0;
	if ($now - $last < 1) {
		//header ( "Location: $file" );
		include ($file);
		exit ();
	} else {
		ob_start ();
		
		?>
<?
require_once("../include/db_info.inc.php");

if(isset($OJ_LANG)){
		require_once("../lang/$OJ_LANG.php");
}
require_once("../include/const.inc.php");
require_once("../include/my_func.inc.php");
class TM{
	var $solved=0;
	var $time=0;
	var $p_wa_num;
	var $p_ac_sec;
	var $user_id;
        var $nick;
	function TM(){
		$this->solved=0;
		$this->time=0;
		$this->p_wa_num=array(0);
		$this->p_ac_sec=array(0);
	}
	function Add($pid,$sec,$res){
//		echo "Add $pid $sec $res<br>";
		if (isset($this->p_ac_sec[$pid])&&$this->p_ac_sec[$pid]>0)
			return;
		if ($res!=4){
			if(isset($this->p_wa_num[$pid])){
				$this->p_wa_num[$pid]++;
			}else{
				$this->p_wa_num[$pid]=1;
			}
		}else{
			$this->p_ac_sec[$pid]=$sec;
			$this->solved++;
			$this->time+=$sec+$this->p_wa_num[$pid]*1200;
//			echo "Time:".$this->time."<br>";
//			echo "Solved:".$this->solved."<br>";
		}
	}
}

function s_cmp($A,$B){
//	echo "Cmp....<br>";
	if ($A->solved!=$B->solved) return $A->solved<$B->solved;
	else return $A->time>$B->time;
}

?>

<!--设置当前导航标签
<script>
var cur=document.getElementById("con_stand_tab");
cur.className = "current";
</script>  
-->

<?
/***********************************************************/
$ccnt = 1;
$bianhao = array(array());
$blue = array(array());
/***********************************************************/
$sql="SELECT `start_time`,`title` FROM `contest` WHERE `contest_id`='$cid'";
$result=mysql_query($sql) or die(mysql_error());
$rows_cnt=mysql_num_rows($result);
$start_time=0;
if ($rows_cnt>0){
	$row=mysql_fetch_array($result);
	$start_time=strtotime($row[0]);
	$title=$row[1];
}
mysql_free_result($result);
if ($start_time==0){
	echo "No Such Contest";
	require_once("../oj-footer.php");
	exit(0);
}

if ($start_time>time()){
	echo "Contest Not Started!";
	require_once("../oj-footer.php");
	exit(0);
}

$sql="SELECT count(1) FROM `contest_problem` WHERE `contest_id`='$cid'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$pid_cnt=intval($row[0]);
mysql_free_result($result);

$sql="SELECT 
	users.user_id,users.nick,solution.result,solution.num,solution.in_date 
		FROM 
			(select * from solution where solution.contest_id='$cid') solution 
		left join users 
		on users.user_id=solution.user_id 
	ORDER BY users.user_id,in_date";
//echo $sql;
$result=mysql_query($sql);
$user_cnt=0;
$user_name='';
$U=array();
while ($row=mysql_fetch_object($result)){
	$n_user=$row->user_id;
	if (strcmp($user_name,$n_user)){
		$user_cnt++;
		$U[$user_cnt]=new TM();
		$U[$user_cnt]->user_id=$row->user_id;
                $U[$user_cnt]->nick=$row->nick;

		$user_name=$n_user;
	}
	$U[$user_cnt]->Add($row->num,strtotime($row->in_date)-$start_time,intval($row->result));
}
mysql_free_result($result);
usort($U,"s_cmp");
$rank=1;
echo "<style> td{font-size:14} </style>";
echo "<title>Contest RankList -- $title</title>";
if(isset($_SESSION['administrator']))
{
echo "<center><h3>Contest RankList -- $title</h3><a href=contestrank.xls.php?cid=$cid>Download</a></center>";
}
echo "<table><tr class=toprow align=center><td width=5%>Rank<td width=10%>User<td width=10%>Nick<td width=5%>Solved<td width=5%>Penalty";
for ($i=0;$i<$pid_cnt;$i++)
	echo "<td><a href=problem.php?cid=$cid&pid=$i>$PID[$i]</a>";
echo "</tr>";
$RRR = mysql_query("SELECT * FROM cr");
$SSS=array('a__','b__','c__','d__','e__','f__','g__','h__','i__','j__','k__','l__','m__','n__','o__','p__','q__','r__','s__');
while($ROWROW = mysql_fetch_array($RRR))
{
	for($i=0;$i<19;$i++)
		$blue[$ROWROW['uid']][$i] = $ROWROW[$SSS[$i]];
	for($i=0;$i<19;$i++)
	{
		$bianhao[$ROWROW['uid']][$i]=$ccnt;
		$ccnt++;
		if(strval($bianhao[$ROWROW['uid']][$i])==$_GET['bj'])
		{
			$blue[$ROWROW['uid']][$i] = '1';
			mysql_query("UPDATE cr SET ".$SSS[$i]." = '1'
			WHERE uid = '".$ROWROW['uid']."'");

		}
		else if(strval($bianhao[$ROWROW['uid']][$i])==$_GET['qxbj'])
		{
			$blue[$ROWROW['uid']][$i] = '0';
			mysql_query("UPDATE cr SET ".$SSS[$i]." = '0'
			WHERE uid = '".$ROWROW['uid']."'");
		}
	}
}


for ($i=0;$i<$user_cnt;$i++){
	if ($i&1) echo "<tr class=oddrow align=center>";
	else echo "<tr class=evenrow align=center>";
	echo "<td>$rank";
	$rank++;
	$uuid=$U[$i]->user_id;
        
	$usolved=$U[$i]->solved;
	echo "<td><a href=userinfo.php?user=$uuid>$uuid</a>";
	echo "<td><a href=userinfo.php?user=$uuid>".$U[$i]->nick."</a>";
	echo "<td><a href=status.php?user_id=$uuid&cid=$cid>$usolved</a>";
	echo "<td>".sec2str($U[$i]->time);
	for ($j=0;$j<$pid_cnt;$j++){
		$bg_color="eeeeee";
/***********************************************************/
		if($blue[$uuid][$j] == '1') {$bg_color="1874CD";}
/***********************************************************/
		else if (isset($U[$i]->p_ac_sec[$j])&&$U[$i]->p_ac_sec[$j]>0){
			$bg_color="aaffaa";
		}else if(isset($U[$i]->p_wa_num[$j])&&$U[$i]->p_wa_num[$j]>0) {
			$bg_color="ffaaaa";
		}
/***********************************************************/
		
		echo "<td bgcolor=$bg_color id=\"".strval($bianhao[$uuid][$j])."\""."onClick='change(this)'>";

			
	//	 $BBB="<script>document.write(oid);</script>"; 
	//		mysql_query("UPDATE cr SET ".$SSS[$i]." = '1'
	//		WHERE uid = '".$uuid."'");
/***********************************************************/
		 
		if(isset($U[$i])){
			if (isset($U[$i]->p_ac_sec[$j])&&$U[$i]->p_ac_sec[$j]>0)
				echo sec2str($U[$i]->p_ac_sec[$j]);
			if (isset($U[$i]->p_wa_num[$j])&&$U[$i]->p_wa_num[$j]>0) 
				echo "(-".$U[$i]->p_wa_num[$j].")";
			echo "【";
			echo $bianhao[$uuid][$j];
			if($blue[$uuid][$j] == '1')
			{echo "气球已发";}
			echo "】";
		}
	}
	echo "</tr>";
}
echo "</table>";

?>
<?php
		if(!file_exists("cache")) mkdir("cache");
		file_put_contents($file,ob_get_contents ());
	}
?>
